package com.ruoyi.framework.web.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.wq.domain.WqNoticeChat;
import com.ruoyi.wq.domain.WqUserRecord;
import com.ruoyi.wq.service.IWqNoticeChatService;
import com.ruoyi.wq.service.IWqUserRecordService;
import com.ruoyi.wx.domain.WxMaForm;
import com.ruoyi.wx.service.WxMaUserService;

@Component
public class UserRecordService {
	
	@Autowired
	private IWqUserRecordService wqUserRecordService;
	
	@Autowired
	private ISysUserService sysUserService;
	
	@Autowired
    private WxMaUserService wxMaUserService;
	
	@Autowired
	private IWqNoticeChatService wqNoticeChatService;

	public WqUserRecord getUserRecordByUserId() {
		Long userId = SecurityUtils.getUserId();
		WqUserRecord userRecord = wqUserRecordService.getById(userId);
		if(userRecord == null){
			SysUser user = sysUserService.getById(userId);
			userRecord = new WqUserRecord();
			userRecord.setUserId(userId);
			userRecord.setAvatar(user.getAvatar());
			userRecord.setAlbums(user.getAlbums());
			userRecord.setNickName(user.getNickName());
			userRecord.setSex(user.getSex());
			userRecord.setAge(user.getAge());
			userRecord.setWechat(user.getWechat());
			userRecord.setSignName(user.getSignName());
			userRecord.setProfession(user.getProfession());
			userRecord.setMarry(user.getMarry());
			userRecord.setBodyHeight(user.getBodyHeight());
			userRecord.setIntroduce(user.getIntroduce());
			userRecord.setHobby(user.getHobby());
			wqUserRecordService.saveOrUpdate(userRecord);
		}
		return userRecord;
	}

	public boolean updateRecord(WqUserRecord wqUserRecord) {
		Long userId = SecurityUtils.getUserId();
		SysUser user = sysUserService.getById(userId);
		
		if(StringUtils.isNotBlank(wqUserRecord.getNickName())){
        	WxMaForm wxMaForm = new WxMaForm();
        	wxMaForm.setAppId(user.getAppId());
        	wxMaForm.setMessage(wqUserRecord.getNickName());
        	wxMaForm.setOpenId(user.getOpenId());
        	Boolean checkMessage = wxMaUserService.checkMessage(wxMaForm);
        	if(!checkMessage){
        		throw new ServiceException("昵称内容违规");
        	}
        	if(StringUtils.getMatch(wqUserRecord.getNickName())){
        		throw new ServiceException("昵称内容违规");
        	}
        }
        
        if(StringUtils.isNotBlank(wqUserRecord.getSignName())){
        	WxMaForm wxMaForm = new WxMaForm();
        	wxMaForm.setAppId(user.getAppId());
        	wxMaForm.setMessage(wqUserRecord.getSignName());
        	wxMaForm.setOpenId(user.getOpenId());
        	Boolean checkMessage = wxMaUserService.checkMessage(wxMaForm);
        	if(!checkMessage){
        		throw new ServiceException("签名内容违规");
        	}
        	if(StringUtils.getMatch(wqUserRecord.getSignName())){
        		throw new ServiceException("签名内容违规");
        	}
        }
        
        if(StringUtils.isNotBlank(wqUserRecord.getIntroduce())){
        	WxMaForm wxMaForm = new WxMaForm();
        	wxMaForm.setAppId(user.getAppId());
        	wxMaForm.setMessage(wqUserRecord.getIntroduce());
        	wxMaForm.setOpenId(user.getOpenId());
        	Boolean checkMessage = wxMaUserService.checkMessage(wxMaForm);
        	if(!checkMessage){
        		throw new ServiceException("关于我内容违规");
        	}
        	if(StringUtils.getMatch(wqUserRecord.getIntroduce())){
        		throw new ServiceException("关于我内容违规");
        	}
        }
        
        if(StringUtils.isNotBlank(wqUserRecord.getHobby())){
        	WxMaForm wxMaForm = new WxMaForm();
        	wxMaForm.setAppId(user.getAppId());
        	wxMaForm.setMessage(wqUserRecord.getHobby());
        	wxMaForm.setOpenId(user.getOpenId());
        	Boolean checkMessage = wxMaUserService.checkMessage(wxMaForm);
        	if(!checkMessage){
        		throw new ServiceException("关于对方内容违规");
        	}
        	if(StringUtils.getMatch(wqUserRecord.getHobby())){
        		throw new ServiceException("关于对方内容违规");
        	}
        }
		
		user.setNickName(wqUserRecord.getNickName());
		user.setAvatar(wqUserRecord.getAvatar());
		user.setAge(wqUserRecord.getAge());
		user.setAlbums(wqUserRecord.getAlbums());
		user.setWechatStatus(wqUserRecord.getWechatStatus());
		if("0".equals(user.getWechatCheck())){
			user.setWechat(wqUserRecord.getWechat());
		}
		user.setBodyHeight(wqUserRecord.getBodyHeight());
		user.setBodyWeight(wqUserRecord.getBodyWeight());
		user.setHealth(wqUserRecord.getHealth());
		user.setLiveCity(wqUserRecord.getLiveCity());
		user.setEducation(wqUserRecord.getEducation());
		user.setMarry(wqUserRecord.getMarry());
		user.setDrink(wqUserRecord.getDrink());
		user.setSmoke(wqUserRecord.getSmoke());
		user.setProfession(wqUserRecord.getProfession());
		user.setSignName(wqUserRecord.getSignName());
		user.setHobby(wqUserRecord.getHobby());
		user.setIntroduce(wqUserRecord.getIntroduce());
		user.setInviteCode(wqUserRecord.getInviteCode());
		user.setIsShow("1");
		sysUserService.updateById(user);
		
		WqNoticeChat noticeChat = new WqNoticeChat();
		noticeChat.setFromUserId(11637079L);
		noticeChat.setToUserId(user.getUserId());
		noticeChat.setNoticeType("SYSTEM");
		noticeChat.setRemark("资料审核通过！");
		wqNoticeChatService.saveOrUpdate(noticeChat);
		
		// 不用审核
		wqUserRecord.setStatus("2");
		wqUserRecordService.updateById(wqUserRecord);
		return true;
	}

}
